﻿Imports System.Data
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class Order
    Implements INotifyPropertyChanged

#Region "Privates"
    Private posi As New ObservableCollection(Of OrderedArticle)
    Private posidel As New Collection(Of String)
    Private _customer As Customer
    Private typ As Dokumentart
    Private tag As New Datum
    Private no As Integer
    Private dt As Collection(Of OrderPreviewModel)
    Private _text As String
    Private _rabatt As Decimal
    Private _festerabzug As New Money(0)
    Private _festerabzugtext As String
    Private _zahlung As PaymentMethode
#End Region

    ''' <summary>
    ''' Methode zum Erstellen eines neuen Auftrags
    ''' </summary>
    ''' <param name="Auftragstyp">Die gewünschte Auftragsart. Standard ist Rechnung</param>
    ''' <remarks></remarks>
    Public Sub New(ByRef orders As Collection(Of OrderPreviewModel), Optional ByVal Auftragstyp As Dokumentart = DokumentArt.Rechnung)
        AddHandler OrderedArticles.CollectionChanged, AddressOf OrderValueRefresh

        _customer = New Customer()
        tag = New Datum(Date.Today)
        DokumentArt = Auftragstyp
        dt = orders
        OrderID = (GenerateNumber(orders))
        SkontoWithSupplementary = False
    End Sub

    ''' <summary>
    ''' Methode zum Erstellen eines neuen Auftrags
    ''' </summary>
    ''' <remarks>Nur zum erstellen einer Liste aus der XML-Datei!!</remarks>
    Public Sub New()
        _customer = New Customer()
        AddHandler OrderedArticles.CollectionChanged, AddressOf OrderValueRefresh
    End Sub

    ''' <summary>
    ''' Substracts a fixed amount 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Reduction As Money
        Get
            Return _festerabzug
        End Get
        Set(value As Money)
            _festerabzug = value
            NotifyPropertyChanged("Reduction")
            ArticleChanged()
        End Set
    End Property

    Public Property ReductionText As String
        Get
            Return _festerabzugtext
        End Get
        Set(value As String)
            _festerabzugtext = value
            NotifyPropertyChanged("ReductionText")
        End Set
    End Property

    Public Property OrderText As String
        Get
            Return _text
        End Get
        Set(value As String)
            _text = value
            NotifyPropertyChanged("OrderText")
        End Set
    End Property

    Public ReadOnly Property TotalWeight() As String
        Get
            Dim gew As Decimal = 0
            For Each art As OrderedArticle In posi
                gew += art.ArticleWeight * art.OrderedAmount
            Next
            Return gew.ToString("0.000")
        End Get
    End Property

    Public ReadOnly Property GetArticle(ByVal ArtikelNummer As String) As OrderedArticle
        Get
            For Each art As Article In Me.OrderedArticles
                If art.ArticleID = ArtikelNummer Then
                    Return art
                End If
            Next
            Return OrderedArticle.EmptyArticle(1)
        End Get
    End Property
    Public ReadOnly Property GetArticlebyGUID(ByVal GUID As String) As OrderedArticle
        Get
            For Each art As OrderedArticle In Me.OrderedArticles
                If art.OrderArticleID = GUID Then
                    Return art
                End If
            Next
            Return OrderedArticle.EmptyArticle(1)
        End Get
    End Property

    Public Property PaymentMethode As PaymentMethode
        Get
            Return _zahlung
        End Get
        Set(value As PaymentMethode)
            _zahlung = value
            NotifyPropertyChanged("PaymentMethode")
            ArticleChanged()
        End Set
    End Property

    Public Property Rebate As Decimal
        Get
            Return _rabatt
        End Get
        Set(value As Decimal)
            _rabatt = value
            NotifyPropertyChanged("Rebate")
        End Set
    End Property

    Public ReadOnly Property OrderedArticles() As ObservableCollection(Of OrderedArticle)
        Get
            Return posi
        End Get
    End Property

    Public Sub DeleteArticle(ByVal artnr As String)
        OrderedArticles.Remove(GetArticle(artnr))
        posidel.Add(artnr)
    End Sub

    Public ReadOnly Property UniqueID As String
        Get
            Return CInt(DokumentArt).ToString + "-" + OrderID.ToString
        End Get
    End Property

    ''' <summary>
    ''' The selected customer to this order
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Customer As Customer
        Get
            Return _customer
        End Get
        Set(ByVal value As Customer)
            _customer = value
            NotifyPropertyChanged("Customer")
        End Set
    End Property

    ''' <summary>
    ''' Generates a unique ID in combination with the document type
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OrderID As Integer
        Get
            Return no
        End Get
        Set(value As Integer)
            Try
                no = CInt(value)
            Catch ex As Exception
                no = -1
            End Try
        End Set
    End Property

    ''' <summary>
    ''' Represents the kind of order. Together with LaufendeNummer it represents a unique combination
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DokumentArt() As Dokumentart
        Get
            Return typ
        End Get
        Set(ByVal value As Dokumentart)
            typ = value
            OrderID = GenerateNumber()
            NotifyPropertyChanged("DokumentArt")
            NotifyPropertyChanged("OrderID")
        End Set
    End Property

    ''' <summary>
    ''' Das Auftragsdatum
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Datum() As Datum
        Get
            Return tag
        End Get
        Set(ByVal value As Datum)
            tag = value
            NotifyPropertyChanged("Datum")
        End Set
    End Property

    Private Sub OrderValueRefresh(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
        If e.Action = Specialized.NotifyCollectionChangedAction.Add Then
            Try
                Dim art As Article = e.NewItems(0)
                AddHandler art.PropertyChanged, AddressOf ArticleChanged
            Catch ex As Exception : End Try
        End If
        If e.Action = Specialized.NotifyCollectionChangedAction.Remove Then
            For i = e.OldStartingIndex To OrderedArticles.Count - 1
                OrderedArticles(i).OrderPosition -= 1
            Next
        End If
        NotifyPropertyChanged("TotalValue")
        NotifyPropertyChanged("OrderValue")
        NotifyPropertyChanged("GoodValue")
        NotifyPropertyChanged("NetValue")
        NotifyPropertyChanged("GrossValue")
        NotifyPropertyChanged("TotalValueWithoutSkonto")
        NotifyPropertyChanged("SkontoValue")
        NotifyPropertyChanged("RebateValue")
        NotifyPropertyChanged("NetValueWithoutSupplementary")
        NotifyPropertyChanged("GrossValueWithoutSupplementary")
        NotifyPropertyChanged("GoodValueFromNet")
        NotifyPropertyChanged("TotalValueFromNet")
        NotifyPropertyChanged("SkontoValueFromNet")
        NotifyPropertyChanged("TotalValueWithoutSkontoFromNet")
    End Sub

    Private Sub ArticleChanged()
        NotifyPropertyChanged("TotalValue")
        NotifyPropertyChanged("OrderValue")
        NotifyPropertyChanged("GoodValue")
        NotifyPropertyChanged("NetValue")
        NotifyPropertyChanged("GrossValue")
        NotifyPropertyChanged("TotalValueWithoutSkonto")
        NotifyPropertyChanged("SkontoValue")
        NotifyPropertyChanged("RebateValue")
        NotifyPropertyChanged("NetValueWithoutSupplementary")
        NotifyPropertyChanged("GrossValueWithoutSupplementary")
        NotifyPropertyChanged("GoodValueFromNet")
        NotifyPropertyChanged("TotalValueFromNet")
        NotifyPropertyChanged("SkontoValueFromNet")
        NotifyPropertyChanged("TotalValueWithoutSkontoFromNet")
    End Sub

    Private Function GenerateNumber(ByVal auftragdaten As Collection(Of OrderPreviewModel)) As Integer
        If auftragdaten Is Nothing Then
            Return 1000
        End If
        Dim start As Integer = 1000
        Dim highestnum As Integer = 0
        Try
            For Each o As OrderPreviewModel In auftragdaten
                Select Case DokumentArt
                    Case DokumentArt.Rechnung
                        If o.OrderTyp = DokumentArt.Rechnung Then
                            If o.OrderID > highestnum Then
                                highestnum = o.OrderID
                            End If
                        End If
                    Case DokumentArt.Lieferschein
                        If o.OrderTyp = DokumentArt.Lieferschein Then
                            If o.OrderID > highestnum Then
                                highestnum = o.OrderID
                            End If
                        End If
                    Case DokumentArt.Angebot
                        If o.OrderTyp = DokumentArt.Angebot Then
                            If o.OrderID > highestnum Then
                                highestnum = o.OrderID
                            End If
                        End If
                    Case DokumentArt.Gutschrift
                        If o.OrderTyp = DokumentArt.Gutschrift Then
                            If o.OrderID > highestnum Then
                                highestnum = o.OrderID
                            End If
                        End If
                End Select
            Next
        Catch ex As Exception
        End Try
        If highestnum = 0 Then
            Return 1000
        Else
            Return highestnum + 1
        End If
    End Function

    Private Function GenerateNumber() As Integer
        Return GenerateNumber(dt)
    End Function

#Region "'### Since Version 1.1 ###'"
    ''' <summary>
    ''' Returns the total order value with taxes and discounts and skonto
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property TotalValue() As Money
        Get
            'basic
            Dim totalprice As New Money(0)
            For Each item In OrderedArticles
                If item.ArticleCategory.Contains("Nebenleistung") = False Then
                    totalprice += item.GrossOrderValue
                End If
            Next
            'Discount
            '### First fixed value, then percent ###'
            totalprice -= Reduction
            totalprice = totalprice * (1 - Rebate)
            'supplementary
            For Each item In OrderedArticles
                If item.ArticleCategory.Contains("Nebenleistung") Then
                    totalprice += item.GrossOrderValue
                End If
            Next
            'Skonto
            If PaymentMethode IsNot Nothing Then
                totalprice *= (1 - PaymentMethode.Reduction)
            End If
            Return totalprice
        End Get
    End Property

    Private _lg, _la, _lr, _ll As String
    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedToCredit As String
        Get
            Return _lg
        End Get
        Set(value As String)
            _lg = value
            NotifyPropertyChanged("LinkedToGutschrift")
        End Set
    End Property

    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedToOffer As String
        Get
            Return _la
        End Get
        Set(value As String)
            _la = value
            NotifyPropertyChanged("LinkedToAngebot")
        End Set
    End Property

    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedToInvoice As String
        Get
            Return _lr
        End Get
        Set(value As String)
            _lr = value
            NotifyPropertyChanged("LinkedToRechnung")
        End Set
    End Property

    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedToDelivery As String
        Get
            Return _ll
        End Get
        Set(value As String)
            _ll = value
            NotifyPropertyChanged("LinkedToLieferschein")
        End Set
    End Property

    Private _lfg, _lfa, _lfr, _lfl As String
    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedFromCredit As String
        Get
            Return _lfg
        End Get
        Set(value As String)
            _lfg = value
            NotifyPropertyChanged("LinkedFromGutschrift")
        End Set
    End Property

    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedFromOffer As String
        Get
            Return _lfa
        End Get
        Set(value As String)
            _lfa = value
            NotifyPropertyChanged("LinkedFromAngebot")
        End Set
    End Property

    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedFromInvoice As String
        Get
            Return _lfr
        End Get
        Set(value As String)
            _lfr = value
            NotifyPropertyChanged("LinkedFromRechnung")
        End Set
    End Property

    ''' <summary>
    ''' Wenn der Auftrag weitergeführt wurde oder ein weiterführender Auftrag ist, wird dessen Nummer als String gespeichert
    ''' </summary>
    ''' <value>Kennung / Nummer des Auftrags</value>
    ''' <returns>Kennung / Nummer des Auftrags</returns>
    ''' <remarks>Den Auftrag an sich muss man mit einer Instanz von FastXML oder XMLHelper noch laden</remarks>
    Public Property LinkedFromDelivery As String
        Get
            Return _lfl
        End Get
        Set(value As String)
            _lfl = value
            NotifyPropertyChanged("LinkedFromLieferschein")
        End Set
    End Property

    Private _rechnungsa, _liefera As Address

    Public Property DeliveryAddress As Address
        Get
            Return _liefera
        End Get
        Set(value As Address)
            _liefera = value
            NotifyPropertyChanged("DeliveryAddress")
        End Set
    End Property
#End Region

#Region "'### Since Version 1.2 ###'"
    Public Overrides Function Equals(obj As Object) As Boolean
        If TypeOf obj Is Order Then
            Dim _obj As Order = obj
            If _obj.DokumentArt = DokumentArt AndAlso _obj.OrderID = OrderID Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' Returns the Field "TotalValue"
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property OrderValue As Money
        Get
            Return TotalValue()
        End Get
    End Property

    ''' <summary>
    ''' Returns the Field "GoodValue"
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property GoodValue As Money
        Get
            Return GrossValue
        End Get
    End Property

    ''' <summary>
    ''' Without taxes (without discount ...). Complete article value
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property NetValue As Money
        Get
            Dim output As New Money(0)
            For Each art As OrderedArticle In posi
                Try
                    output += art.NetOrderValue
                Catch ex As Exception
                    Throw New AuftragFailedException("Could not calculate total price cause one article is not referenced to an object!", ex)
                End Try
            Next
            Return output
        End Get
    End Property

    ''' <summary>
    ''' With taxes (without discount ...). Complete article value
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property GrossValue As Money
        Get
            Dim output As New Money(0)
            For Each art As OrderedArticle In posi
                Try
                    output += art.GrossOrderValue
                Catch ex As Exception
                    Throw New AuftragFailedException("Could not calculate total price cause one article is not referenced to an object!", ex)
                End Try
            Next
            Return output
        End Get
    End Property

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
#End Region

#Region "'### Since Version 1.3 ###'"
    ''' <summary>
    ''' Returns the total order value with taxes and discounts without skonto
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property TotalValueWithoutSkonto() As Money
        Get
            'basic
            Dim totalprice As New Money(0)
            For Each item In OrderedArticles
                If item.ArticleCategory.Contains("Nebenleistung") = False Then
                    totalprice += item.GrossOrderValue
                End If
            Next
            'Discount
            '### First fixed value, then percent ###'
            totalprice -= Reduction
            totalprice = totalprice * (1 - Rebate)
            'supplementary
            For Each item In OrderedArticles
                If item.ArticleCategory.Contains("Nebenleistung") Then
                    totalprice += item.GrossOrderValue
                End If
            Next
            Return totalprice
        End Get
    End Property

    ''' <summary>
    ''' Returns the Skonto Value
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property SkontoValue As Money
        Get
            Dim temp = TotalValueWithoutSkonto
            Try
                temp *= PaymentMethode.Reduction
            Catch ex As Exception
                temp.Amount = 0
            End Try
            Return temp
        End Get
    End Property

    ''' <summary>
    ''' Absolute amount of the rebate based on "GrossValueWithoutSupplementary"
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property RebateValue As Money
        Get
            'basic
            Dim totalprice As New Money(0)
            For Each item In OrderedArticles
                If item.ArticleCategory.Contains("Nebenleistung") = False Then
                    totalprice += item.GrossOrderValue
                End If
            Next
            'Discount
            '### First fixed value, then percent ###'
            totalprice -= Reduction

            Dim result As Money = totalprice * Rebate
            Return result
        End Get
    End Property

    ''' <summary>
    ''' Without taxes (without discount and supplementary work ...). Complete article value
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property NetValueWithoutSupplementary As Money
        Get
            Dim output As New Money(0)
            For Each art As OrderedArticle In posi
                Try
                    If art.ArticleCategory.Contains("Nebenleistung") = False Then
                        output += art.NetOrderValue
                    End If
                Catch ex As Exception
                    Throw New AuftragFailedException("Could not calculate total price cause one article is not referenced to an object!", ex)
                End Try
            Next
            Return output
        End Get
    End Property

    ''' <summary>
    ''' With taxes (without discount and supplementary work...). Complete article value
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property GrossValueWithoutSupplementary As Money
        Get
            Dim output As New Money(0)
            For Each art As OrderedArticle In posi
                Try
                    If art.ArticleCategory.Contains("Nebenleistung") = False Then
                        output += art.GrossOrderValue
                    End If
                Catch ex As Exception
                    Throw New AuftragFailedException("Could not calculate total price cause one article is not referenced to an object!", ex)
                End Try
            Next
            Return output
        End Get
    End Property
#End Region

#Region "'### Since Version 1.4 ###'"
    Private _textbefore As String
    Private _lasteditby As nHibernate.Models.UserPreviewModel
    Public Property TextBefore As String
        Get
            Return _textbefore
        End Get
        Set(value As String)
            _textbefore = value
            NotifyPropertyChanged("TextBefore")
        End Set
    End Property

    Public Property LastEditBy As nHibernate.Models.UserPreviewModel
        Get
            Return _lasteditby
        End Get
        Set(value As nHibernate.Models.UserPreviewModel)
            _lasteditby = value
            NotifyPropertyChanged("LastEditBy")
        End Set
    End Property
#End Region

#Region "'### Since Version 1.5 ###'"
    ''' <summary>
    ''' Returns the total order value with taxes and discounts without skonto. Use this, if you have a net invoice
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property TotalValueWithoutSkontoFromNet() As Money
        Get
            'basic
            Dim totalprice As New Money(0)
            Dim list As New List(Of TaxClassMoneyModel)
            Dim list2 As New List(Of TaxClassMoneyModel)
            For Each item In OrderedArticles
                If Not item.ArticleCategory.Contains("Nebenleistung") Then
                    If list.Contains(New TaxClassMoneyModel With {.TaxClass = item.TaxClass}) Then
                        list(list.IndexOf(New TaxClassMoneyModel With {.TaxClass = item.TaxClass})).Money += item.NetOrderValue
                    Else
                        list.Add(New TaxClassMoneyModel With {.TaxClass = item.TaxClass, .Money = item.NetOrderValue})
                    End If
                Else
                    If list2.Contains(New TaxClassMoneyModel With {.TaxClass = item.TaxClass}) Then
                        list2(list2.IndexOf(New TaxClassMoneyModel With {.TaxClass = item.TaxClass})).Money += item.NetOrderValue
                    Else
                        list2.Add(New TaxClassMoneyModel With {.TaxClass = item.TaxClass, .Money = item.NetOrderValue})
                    End If
                End If
            Next
            For Each item In list
                totalprice += item.Money * ((item.TaxClass.TaxRate + 100) / 100)
            Next
            'Discount
            '### First fixed value, then percent ###'
            totalprice -= Reduction
            totalprice = totalprice * (1 - Rebate)
            For Each item In list2
                totalprice += item.Money * ((item.TaxClass.TaxRate + 100) / 100)
            Next

            Return totalprice
        End Get
    End Property

    Public ReadOnly Property SkontoValueFromNet As Money
        Get
            If SkontoWithSupplementary Then
                Dim temp = TotalValueWithoutSkontoFromNet
                Try
                    temp *= PaymentMethode.Reduction
                Catch ex As Exception
                    temp.Amount = 0
                End Try
                Return temp
            Else
                Dim totalprice As New Money(0)
                Dim list As New List(Of TaxClassMoneyModel)
                For Each item In OrderedArticles
                    If Not item.ArticleCategory.Contains("Nebenleistung") Then
                        If list.Contains(New TaxClassMoneyModel With {.TaxClass = item.TaxClass}) Then
                            list(list.IndexOf(New TaxClassMoneyModel With {.TaxClass = item.TaxClass})).Money += item.NetOrderValue
                        Else
                            list.Add(New TaxClassMoneyModel With {.TaxClass = item.TaxClass, .Money = item.NetOrderValue})
                        End If
                    End If
                Next
                For Each item In list
                    totalprice = item.Money * ((item.TaxClass.TaxRate + 100) / 100)
                Next
                Return totalprice * PaymentMethode.Reduction
            End If
        End Get
    End Property

    Public ReadOnly Property TotalValueFromNet() As Money
        Get
            Return TotalValueWithoutSkontoFromNet - SkontoValueFromNet
        End Get
    End Property

    Public ReadOnly Property GoodValueFromNet As Money
        Get
            Dim totalprice As New Money(0)
            Dim list As New List(Of TaxClassMoneyModel)
            For Each item In OrderedArticles
                If list.Contains(New TaxClassMoneyModel With {.TaxClass = item.TaxClass}) Then
                    list(list.IndexOf(New TaxClassMoneyModel With {.TaxClass = item.TaxClass})).Money += item.NetOrderValue
                Else
                    list.Add(New TaxClassMoneyModel With {.TaxClass = item.TaxClass, .Money = item.NetOrderValue})
                End If
            Next
            For Each item In list
                totalprice = item.Money * ((item.TaxClass.TaxRate + 100) / 100)
            Next
            Return totalprice
        End Get
    End Property

#End Region

#Region "Since Version 1.6"
    Public Property SkontoWithSupplementary As Boolean
#End Region
End Class

Public Class TaxClassMoneyModel
    Public Property TaxClass As TaxClass
    Public Property Money As Money

    Public Overrides Function Equals(obj As Object) As Boolean
        Return TaxClass.Equals(DirectCast(obj, TaxClassMoneyModel).TaxClass)
    End Function
End Class